package com.linkage.order.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.user.interceptor.NeedLogin;
import com.linkage.user.service.LoginService;

/**
 * 登录controller.
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 2014-11-27 15:09
 */
@Controller
@RequestMapping("/login6")
public class LoginController6 {
    @Autowired
    private LoginService loginService;

    /**
     * 首页
     *
     * @return
     */
    @NeedLogin
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    /**
     * 进入登录页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoLogin", method = RequestMethod.GET)
    public String gotoLogin() {
        return "login/login_form";
    }

    /**
     * 登录操作
     *
     * @return
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public String login(@RequestParam("loginName") String loginName, @RequestParam("password") String password,
            HttpServletRequest request, Model model) {
        boolean login = this.loginService.login(request, loginName, password);
        if (login) {
            return "success";
        } else {
            model.addAttribute("errorMsg", "登录失败！");
            return "login/login_form";
        }
    }

    @RequestMapping(value = "/success", method = RequestMethod.GET)
    @NeedLogin
    public String success() {
        return "success";
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @NeedLogin
    public String logout(HttpServletRequest request) {
        this.loginService.logout(request);
        return "redirect:/gotoLogin";
    }
}
